<?php

namespace App\Http\Models;

trait SafeSave
{
    /**
     * @deprecated - 完全接受用户输入信息不安全
     *
     * @param array $attributes
     * @param array $options
     *
     * @return bool
     */
    public function safeUpdate(array $attributes = [], array $options = [])
    {
        $table_columns_array = \Schema::getColumnListing($this->getTable());
        $this->fillable($table_columns_array);

        if (! $this->exists) {
            return false;
        }

        return $this->fill($attributes)->save($options);
    }

    /**
     * @deprecated - 完全接受用户输入信息不安全
     *
     * @param array $attributes
     *
     * @return mixed
     */
    public static function safeCreate(array $attributes = [])
    {
        $table_columns_array = \Schema::getColumnListing((new static)->getTable());

        $fillable_attributes = collect($attributes)
            ->only($table_columns_array)
            ->all();

        return self::create($fillable_attributes);
    }
}
